package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.RoleService;
import com.erpanderp.wam.adm.model.entity.RoleServiceGroup;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;
import com.erpanderp.wam.util.HibernateUtil;

public class RoleServicegroupDAO {
	public List<ServiceGroup> getRoleServicegroups(Long employeeId, Long moduleId){
		List<ServiceGroup> serviceGroups = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "select rsg.serviceGroup from RoleServiceGroup rsg where rsg.role.roleId in (select er.role.roleId from EmployeeRole er where er.employee.employeeId = :employeeId) and rsg.serviceGroup.module.moduleId = :moduleId";
			Query query = session.createQuery(queryString).setParameter("moduleId", moduleId).setParameter("employeeId", employeeId);
			query.setCacheable(false);
			
			serviceGroups = (List<ServiceGroup>) query.list();	
			
			session.close();
			return serviceGroups;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}	

	public List<RoleServiceGroup> getRoleServicegroupsByRoleId(Long roleId) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RoleServiceGroup rs where rs.role.roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			return (List<RoleServiceGroup>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public RoleServiceGroup getRoleServicegroup(Long roleId, Long serviceGroupId) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RoleServiceGroup rs where rs.role.roleId=:roleId and rs.serviceGroup.serviceGroupId = :serviceGroupId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId).setLong("serviceGroupId", serviceGroupId);
			query.setCacheable(false);
			List temp = (List<RoleServiceGroup>) query.list();
			if(temp != null && temp.size() > 0){
				System.out.println("ROLE SG ID" + ((RoleServiceGroup) temp.get(0)).getRoleServiceGroupId());
				return (RoleServiceGroup) temp.get(0);
			}else{
				return null;
			}
				
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean deleteRoleServiceGroups(List<RoleServiceGroup> roleServiceGroups) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			for(RoleServiceGroup roleServiceGroup : roleServiceGroups){
				session.delete(roleServiceGroup);	
			}
			session.getTransaction().commit();
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String a[]){
		for(ServiceGroup group : new RoleServicegroupDAO().getRoleServicegroups(1l, 1l)){
			System.out.println(group.getServicegroupName());
		}
	} 
}
